//
//  MyChartingTopView.m
//  intelligent
//
//  Created by 甘世清 on 2024/3/3.
//

#import "MyChartingTopView.h"
#import "ChartingTop2CollectionViewCell.h"
@implementation MyChartingTopView

- (void)SQ_UIViewForSelfView{
    [self.collectionView registerClass:[ChartingTop2CollectionViewCell class] forCellWithReuseIdentifier:@"ChartingTop2CollectionViewCell"];
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}
- (void)setJsonArray:(NSMutableArray *)jsonArray{
    _jsonArray = jsonArray;
    for (HotModel * model in self.jsonArray) {
        if (model.isSeleted) {
            self.seletedModel = model;
        }
    }
    [self.collectionView reloadData];
}
#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
 
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.jsonArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    
    ChartingTop2CollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ChartingTop2CollectionViewCell" forIndexPath:indexPath];
    cell.model = [self.jsonArray objectAtIndex:indexPath.item];
    cell.nameLabel.text = cell.model.title;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    return CGSizeMake(70, 30);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
   
    return 12;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 12, 10, 12);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HotModel * model =  [self.jsonArray objectAtIndex:indexPath.item];
    for (HotModel * model1 in self.jsonArray) {
        model1.isSeleted = NO;
    }
    model.isSeleted = YES;
    self.seletedModel = model;
    [self.collectionView reloadData];
    if (self.seletedBlock) {
        self.seletedBlock(self.seletedModel);
    } 
}

 
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
//        _collectionView.userInteractionEnabled = YES;
        _collectionView.backgroundColor = [UIColor clearColor];
//        _collectionView.alwaysBounceVertical = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}
@end
